﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL;

namespace WebInvoicingsys
{
    public partial class master : System.Web.UI.MasterPage
    {
        string res_script = @" function refresh_session() {
                                   $.ajax({
                                        type: 'post',
                                        url: '/refresh.aspx',
                                        data:'',
                                        success: function (result) {
                                            return;
                                        }
                                    });
                                 }
                               window.setInterval('refresh_session()', 10 * 6000);  //每隔60秒";

        private void Bindmenu()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "";

            //系统处在初始化状态，菜单不授控制
            if (Session["Init"].ToString() == "1")
            {
                sql = "select * from v_menus_levels1 order by sortindex";
            }
            else
            {
                sql = @"select d.menuid, d.menuremark, d.url, d.levels, d.sortindex, d.sub
                          from sysuser a, sysuserrule b, rulemenu c, v_menus_levels1 d
                           where a.userid = b.userid
                            and b.rulegroupid=c.rulegroupid
                            and c.menuid= d.menuid
                            and a.userid='{0}'
                            and c.stu1='1'
                          order by d.sortindex";

                sql = String.Format(sql, Session["userid"].ToString());
            }

            DataSet ds = exec.ExecSQLQueryDataSet(sql);

            if (ds == null)
            {
                Response.Redirect("/default.aspx");
                return;
            }

            //绑定菜单
            DataTable dt = new DataTable("menus");

            dt.Columns.Add("menuid");
            dt.Columns.Add("menuremark");
            dt.Columns.Add("url");
            dt.Columns.Add("sub");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["menuid"] = dr["menuid"].ToString();
                drnew["menuremark"] = dr["menuremark"].ToString();
                drnew["url"] = dr["url"].ToString();
                drnew["sub"] = dr["sub"].ToString();

                dt.Rows.Add(drnew);
            }

            replevel1.DataSource = dt;
            replevel1.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string login = Session["login"].ToString();

                if (login.ToUpper() != "1")
                {
                    Response.Redirect("/default.aspx", false);
                    return;
                }
            }
            catch
            {
                Response.Redirect("/default.aspx", false);
                return;
            }

            if(Session["keeponline"].ToString() == "1")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "refresh_session", res_script, true);
            }

            if (!IsPostBack)
            {
                Bindmenu();
            }

        }

        //绑定二级菜单
        protected void replevel1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Repeater rpColumnNews = (Repeater)e.Item.FindControl("replevel2");

            //找到分类Repeater关联的数据项 
            DataRowView rowv = (DataRowView)e.Item.DataItem;

            //提取分类ID 
            string subParentID = Convert.ToString(rowv["menuid"]);

            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "";

            //系统处在初始化状态，菜单不授控制
            if (Session["Init"].ToString() == "1")
            {
                sql = String.Format("select * from v_menus_levels2 where parentid='{0}' and levels='2' order by parentid, sortindex", subParentID);
            }
            else
            {
                sql = @"select d.menuid, d.menuremark, d.url, d.levels, d.parentid, d.sortindex, d.sub
                          from sysuser a, sysuserrule b, rulemenu c, v_menus_levels2 d
                           where a.userid = b.userid
                            and b.rulegroupid=c.rulegroupid
                            and c.menuid= d.menuid
                            and a.userid='{1}'
                            and c.stu1='1'
                            and d.parentid='{0}' and d.levels='2' order by d.parentid, d.sortindex";

                sql = String.Format(sql, subParentID, Session["userid"].ToString());
            }

            DataSet ds = exec.ExecSQLQueryDataSet(sql);

            if (ds == null)
            {
                Response.Redirect("/default.aspx");
                return;
            }

            //绑定菜单
            DataTable dt = new DataTable("menus");

            dt.Columns.Add("menuid");
            dt.Columns.Add("menuremark");
            dt.Columns.Add("url");
            dt.Columns.Add("sub");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["menuid"] = dr["menuid"].ToString();
                drnew["menuremark"] = dr["menuremark"].ToString();
                drnew["url"] = dr["url"].ToString();
                drnew["sub"] = dr["sub"].ToString();

                dt.Rows.Add(drnew);
            }

            rpColumnNews.DataSource = dt;
            rpColumnNews.DataBind();
        }

        //绑定三级菜单
        protected void replevel2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Repeater rpColumnNews = (Repeater)e.Item.FindControl("replevel3");

            //找到分类Repeater关联的数据项 
            DataRowView rowv = (DataRowView)e.Item.DataItem;

            if (rowv == null)
            {
                return;
            }

            //提取分类ID 
            string subParentID = Convert.ToString(rowv["menuid"]);

            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "";

            //系统处在初始化状态，菜单不授控制
            if (Session["Init"].ToString() == "1")
            {
                sql = String.Format("select * from v_menus_levels3 where parentid='{0}' and levels='3' order by parentid, sortindex", subParentID);
            }
            else
            {
                sql = @"select d.menuid, d.menuremark, d.url, d.levels, d.parentid, d.sortindex
                          from sysuser a, sysuserrule b, rulemenu c, v_menus_levels3 d
                           where a.userid = b.userid
                            and b.rulegroupid=c.rulegroupid
                            and c.menuid= d.menuid
                            and a.userid='{1}'
                            and c.stu1='1'
                            and d.parentid='{0}' and d.levels='3' order by d.parentid, d.sortindex";

                sql = String.Format(sql, subParentID, Session["userid"].ToString());
            }

            DataSet ds = exec.ExecSQLQueryDataSet(sql);

            if (ds == null)
            {
                Response.Redirect("/default.aspx");
                return;
            }

            //绑定菜单
            DataTable dt = new DataTable("menus");

            dt.Columns.Add("menuid");
            dt.Columns.Add("menuremark");
            dt.Columns.Add("url");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["menuid"] = dr["menuid"].ToString();
                drnew["menuremark"] = dr["menuremark"].ToString();
                drnew["url"] = dr["url"].ToString();

                dt.Rows.Add(drnew);
            }

            rpColumnNews.DataSource = dt;
            rpColumnNews.DataBind();
        }
    }
}


